<!--申请审批页-->
<template>
  <!-- 顶部条 -->
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">审批列表</span>
  </div>
  <!-- 审批搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="用车人">
        <el-input placeholder="请输入用车人" style="width:220px;"
        v-model="search.username" @keydown.enter.prevent="searchAudit"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="searchAudit">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 审批单主体 -->
  <el-card style="margin:20px;">
    <!--  审批状态项  -->
    <el-radio-group style="margin-bottom: 15px;" v-model="type" @change="searchAudit">
      <el-radio-button value="10" size="large">待我审核</el-radio-button>
      <el-radio-button value="20" size="large">待他人审核</el-radio-button>
      <el-radio-button value="30" size="large">已审核</el-radio-button>
      <el-radio-button value="40" size="large">驳回</el-radio-button>
    </el-radio-group>
    <!--  审批列表  -->
    <el-table :data="auditList">
      <el-table-column label="编号" prop="id" align="center" width="55" type="index"></el-table-column>
      <el-table-column label="用车人" prop="username" align="center" width="110"></el-table-column>
      <el-table-column label="开始时间" prop="startTime"  align="center"></el-table-column>
      <el-table-column label="结束时间" prop="endTime"  align="center"></el-table-column>
      <el-table-column label="用车事由" prop="reason"  align="center"></el-table-column>
      <el-table-column label="审批人" prop="auditUsernameList"  align="center"></el-table-column>
      <el-table-column label="出发地" prop="departureAddr"  align="center"></el-table-column>
      <el-table-column label="目的地" prop="destinationAddr"  align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center" v-if="type==10||type==40" :key="audit">
        <template #default="scope">
          <!-- 审批10：在待我审核页签下显示，需要当前登录用户进行审批 -->
          <el-button type="primary" link v-if="type==10" @click="auditing(scope.row.id)">审批</el-button>
          <!-- 查看40：在驳回页签下显示，可以查看已驳回申请的驳回原因 -->
          <el-button type="primary" link v-if="type==40" @click="auditing(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 审批/查看弹窗 -->
  <el-dialog :title="dialogTitle" v-model="auditDialogVisible">
    <el-descriptions direction="horizontal" :column="2" border>
      <el-descriptions-item label="用车人">{{auditDialogData.username}}</el-descriptions-item>
      <el-descriptions-item label="用车事由">{{auditDialogData.reason}}</el-descriptions-item>
      <el-descriptions-item label="使用开始时间">{{auditDialogData.startTime}}</el-descriptions-item>
      <el-descriptions-item label="使用结束时间">{{auditDialogData.endTime}}</el-descriptions-item>
      <el-descriptions-item label="车辆出发地">{{auditDialogData.departureAddr}}</el-descriptions-item>
      <el-descriptions-item label="车辆目的地">{{auditDialogData.destinationAddr}}</el-descriptions-item>
      <el-descriptions-item label="驾照图片">
        <img :src="`http://localhost:8080${auditDialogData.imgUrl}`" style="width:150px;" alt="驾照图片">
      </el-descriptions-item>
      <el-descriptions-item label="备注">{{auditDialogData.remark}}</el-descriptions-item>
      <el-descriptions-item label="驳回原因"
      v-if="auditDialogData.auditStatus == 40">{{auditDialogData.rejectReason}}</el-descriptions-item>

      <!--  驳回原因要在点击驳回才显示 -->
      <!--  <el-descriptions-item label="驳回原因">暂无可用车辆</el-descriptions-item> -->
    </el-descriptions>
    <template #footer v-if="auditDialogData.auditStatus==10">
      <el-button @click="auditDialogVisible=false">取消</el-button>
      <el-button type="primary" plain @click="rejectInnerDialogVisible=true">驳回</el-button>
      <el-button type="primary" @click="auditPass()">通过</el-button>
    </template>
  </el-dialog>

  <!-- 驳回原因弹窗 -->
  <el-dialog title="驳回 查看" v-model="rejectInnerDialogVisible" style="margin-top: 37vh;"
             :before-close="handleInnerDialogVisible">
    <el-descriptions direction="horizontal" border>
      <el-descriptions-item label="驳回原因">
        <el-input placeholder="请输入驳回原因" v-model="rejectReason"></el-input>
      </el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <el-button type="primary" plain @click="handleInnerDialogVisible()">取消</el-button>
      <el-button type="primary" @click="auditReject()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
// **************************************************************************************************************************************
// 这是搜索条件以及查询的显示内容
// **************************************************************************************************************************************
// 这是一个对象来保存搜索条件
const search = ref({username:''});
// 定义上一个数组用来保存搜索结果
const auditList = ref([]);
// 获取到当前用户的信息
const user = ref(getUser());
// 定义变量用来保存审批单单选组的值
// 这里要注意加不加双引号就是数字和字符串的区别，加上双引号是字符串
// 这里要注意的是他是一个没有明确的对象，新的值进来以后就会直接替代
const type = ref("10");
//这是一个方法来执行搜索审批单的方法
const searchAudit = () => {
  // 想要查询上一个人，不能用名字因为名字是会重复的，但是id就是不会
  search.value.auditUserId = user.value.id;
  search.value.auditStatus = type.value;
  console.log(search.value);
  let data =qs.stringify(search.value);
  axios.get(BASE_URL+'v1/audit/select?'+data)
      .then((response)=>{
        if (response.data.code==2000){
          auditList.value = response.data.data;
          console.log(auditList.value);
        }else {
          ElMessage.error(response.data.message);
        }
      })
};
// 这是一个重置搜索的方法
const resetSearch = () => {
  // 这里要注意只是清空上username，其他的搜索条件保留
  search.value = {username:  ''};
  searchAudit();
}
// 一进页面就进行加载搜索
onMounted(()=>{
  searchAudit();
})
// ****************************************************************************************************************************************
// 操作的弹窗页面
// ****************************************************************************************************************************************
//控制审批弹窗是否显示
const auditDialogVisible = ref(false);
//控制审批弹窗标题
const dialogTitle = ref("待审批详情");
// 定义用来保存审批弹窗表单的数据
const auditDialogData = ref({
  username: '',
  reason: '',
  startTime: '',
  endTime: '',
  departureAddr: '',
  destinationAddr: '',
  imgUrl: '',
  remark: '',
  rejectReason: ''
})
//待我审核的操作方法,以及驳回操作的 方法
const auditing = (id) => {
  console.log(id);
  auditDialogVisible.value=true;
axios.get(BASE_URL+'v1/audit/select1?id='+id)
    .then((response)=>{
      if (response.data.code==2000){
        auditDialogData.value = response.data.data[0];
        console.log(auditDialogData.value);
        console.log("***************************************************************************");
       if(auditDialogData.value.auditStatus==10){
         dialogTitle.value = "待审批详情";
       }else {
         dialogTitle.value = "驳回详情";
       }
      }else {
        ElMessage.error(response.data.message);
      }
    });
}
// 定义审批通过的方法
const auditPass =()=>{
  auditDialogData.value.auditStatus = 30;
  let data =qs.stringify(auditDialogData.value);
  axios.post(BASE_URL+'v1/audit/update',data)
      .then((response)=>{
        if (response.data.code==2000){
          ElMessage.success('审批通过!');
          auditDialogVisible.value = false;
          auditDialogData.value = {};
          searchAudit();
        }else {
          ElMessage.error(response.data.message);
        }
      })

}
//控制驳回原因弹窗是否显示
const rejectInnerDialogVisible = ref(false);
// 变量用来保存驳回原因
const rejectReason = ref('');

// 定义一个审批驳回的处理方法
const auditReject =()=>{
  rejectInnerDialogVisible.value = true;
  // 这里惊醒前端的非空校验
  if (!rejectReason.value || rejectReason.value.trim() == ''){
    ElMessage.error('请输入驳回原因!');
    return;
  }

  auditDialogData.value.auditStatus = 40;
  auditDialogData.value.rejectReason = rejectReason.value;
  console.log(auditDialogData.value);
let data =qs.stringify(auditDialogData.value);
axios.post(BASE_URL+'v1/audit/update',data)
    .then((response)=>{
      if (response.data.code==2000){
        ElMessage.success('审批驳回!');
        rejectInnerDialogVisible.value = false;
        rejectReason.value = '';
        auditDialogVisible.value = false;
        auditDialogData.value = {};
        searchAudit();
      }else {
        ElMessage.error(response.data.message);
      }
    })
}

// 这是一个关闭内弹窗的方法
const handleInnerDialogVisible = () =>{
  if(confirm('您确认要取消驳回吗?')){
    rejectReason.value = '';//清空驳回原因
    rejectInnerDialogVisible.value = false;
  }
}




</script>

<style>

</style>